<?php

class sess {

	var $zam;
	var $user = array();
	
	#=============================
	# Start session
	#=============================
	function init_session()
	{
		# Load session
		$this->loadsession();
			
		if ( !$this->is_logged_in() )
		{
			$this->setsession( array(
				'member_id'	=>	0,
				'suser'		=>	'Guest'
			) );
		}
		else
		{	
			$this->setsession( array(
				'member_id'	=>	$this->user['member_id'],
				'suser'		=>	$this->user['suser']
			) );
		}
	}
	
	#=============================
	# Are we logged in
	#=============================
	function is_logged_in()
	{
		$where = sprintf("m.id = '%s' AND m.passhash = '%s' AND m.login_key = '%s' AND s.sid = '%s'",
			$this->zam->data['cookie']['zam_mid'],
			$this->zam->data['cookie']['zam_passhash'],
			$this->zam->data['cookie']['zam_loginkey'],
			$this->zam->data['cookie']['zam_sid']
		);
		
		$result = $this->zam->sql->construct( array(
			'action'	=>	'select',
			'fields'	=>	'm.id, m.passhash, m.login_key, s.sid',
			'db'		=>	constant('DB_MEMBERS') . ' m',
			'join'		=>	constant('DB_SESSIONS') . ' s ON(m.id = s.member_id)',
			'where'		=>	$where,
			'limit'		=> 	1
		) );
		
		if ( $this->zam->sql->num_rows( $result ) == 1 )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	#=============================
	# Load user session data
	#=============================
	function loadsession()
	{
		if ( !$this->is_logged_in() )
		{
			$result = $this->zam->sql->construct( array(
				'action'	=>	'select',
				'db'		=>	constant('DB_SESSIONS'),
				'where'		=>	"sid = '{$this->zam->data['cookie']['zam_sid']}'",
				'limit'		=> 	1
			) );
		}
		else
		{
			$where = sprintf("m.id = '%s' AND m.passhash = '%s' AND m.login_key = '%s' AND s.sid = '%s'",
				$this->zam->data['cookie']['zam_mid'],
				$this->zam->data['cookie']['zam_passhash'],
				$this->zam->data['cookie']['zam_loginkey'],
				$this->zam->data['cookie']['zam_sid']
			);
		
			$result = $this->zam->sql->construct( array(
				'action'	=>	'select',
				'fields'	=>	'm.*, s.*',
				'db'		=>	constant('DB_MEMBERS') . ' m',
				'join'		=>	constant('DB_SESSIONS') . ' s ON(m.id = s.member_id)',
				'where'		=>	$where,
				'limit'		=> 	1
			) );
		}
		
		# Fetch user data
		$member = $this->zam->sql->fetch();
		$this->user = $member;
	}
	
	#=============================
	# Cleanup old sessions
	#=============================
	function cleanup_sessions()
	{
		$result = $this->zam->sql->construct( array(
			'action'	=>	'delete',
			'db'		=>	constant('DB_SESSIONS'),
			'where'		=>	'(stime + 900) <= ' . time(),
			'limit'		=> 	1
		) );
	}
	
	#=============================
	# Set cookie faster to use
	#=============================
	function setcookie($key, $value, $expires)
	{
		@setcookie( 'zam_' . $key, $value, (time() + $expires));
	}
	
	#=============================
	# Create a session
	#=============================
	function setsession($data)
	{
		$result = $this->zam->sql->construct( array(
			'action'	=>	'select',
			'fields'	=>	'sid, sip',
			'db'		=>	constant('DB_SESSIONS'),
			'where'		=>	"sid = '{$this->zam->data['cookie']['zam_sid']}' AND sip = '{$_SERVER['REMOTE_ADDR']}'",
			'limit'		=>	1
		) );
		
		# Fields to update
		$fields = array();
			
		while ( list($key, $value) = each($data) )
		{
			$fields[$key] = $value;
		}
		
		$location = ( empty($this->zam->data['params'][1]) ) ? 'home' : $this->zam->data['params'][1];
		
		# Valid session?
		if ( ( empty($this->zam->data['cookie']['zam_sid']) ) or ( $this->zam->sql->num_rows( $result ) == 0 ) )
		{
			# New session
			$sid = sha1( md5( uniqid( rand(), true ) ) );
			$this->setcookie('sid', $sid, 900);
			
			$insert = array(
				'sid'		=>	$sid,
				'slang'		=>	'en',
				'sip'		=>	$_SERVER['REMOTE_ADDR'],
				'slocation'	=>	$location,
				'stime'		=>	time()
			);
			
			$fields = array_merge($fields, $insert);
			
			$this->zam->sql->construct( array(
				'action'	=>	'insert',
				'db'		=>	constant('DB_SESSIONS'),
				'insert'	=>	$fields
			) );
		}
		else
		{
			$this->setcookie('sid', $this->zam->data['cookie']['zam_sid'], 900);
			
			$update = array(
				'sip' 		=> 	$_SERVER['REMOTE_ADDR'],
				'slocation'	=>	$location,
				'stime'		=> 	time()
			);
			
			$fields = array_merge($fields, $update);
			
			$this->zam->sql->construct( array(
				'action'	=>	'update',
				'db'		=>	constant('DB_SESSIONS'),
				'set'		=>	$fields,
				'where'		=>	"sid = '{$this->zam->data['cookie']['zam_sid']}' AND sip = '{$_SERVER['REMOTE_ADDR']}'",
				'limit'		=>	1
			) );
		}
	}
	
}

?>